package top.fengleifeng.question;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : fengelfeing
 * @Package : top.fengleifeng.question
 * @Description:
 * @Date: 2020年10月29日8:38 下午
 */
public class Num290单词规律 {

    class Solution {
        public boolean wordPattern(String pattern, String s) {
            char[] chars= pattern.toCharArray();
            String[] strings = s.split(" ");
            if(chars.length!=strings.length){
                return false;
            }
            Map<Character,String> map =new HashMap<>();
            Map<String,Character> map2 =new HashMap<>();
            for (int i = 0; i < chars.length; i++) {
                char now = chars[i];
                String check = map.get(now);
                String checkStr = strings[i];
                if(null==check){
                    if(map2.get(checkStr)!=null){
                        return false;
                    }
                    map.put(now,strings[i]);
                    map2.put(strings[i],now);
                    continue;
                }else{
                    if(!check.equals(strings[i])){
                        return false;
                    }
                }
            }
            return true;
        }
    }

    public static void main(String[] args) {
//        String s = "abba";
//        String x = "dog dog dog dog";
        String s = "aaa";
        String x ="aa aa aa aa";
        boolean hint = new Num290单词规律().new Solution().wordPattern(s, x);
        System.out.println(hint);
    }
}
